import router from './router'
import store from './store'
import axios from 'axios'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { isRelogin } from '@/utils/request'
import request from '@/utils/request'
import { getToken, setToken } from '@/utils/auth'
import { casLogin } from '@/api/login';

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/auth-redirect', '/bind', '/register']

const defaultSettings = require('./settings.js')

router.beforeEach((to, from, next) => {
  NProgress.start()
  console.log("getToken()1112",getToken())
  if (getToken()) {
    // to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
    /* has token*/
    // if (to.path === '/login') {
    //   next({ path: '/' })
    //   NProgress.done()
    // } else {
      if (store.getters.roles.length === 0) {
        console.log(getToken(), "getToken()11123")
        // isRelogin.show = true
        // 判断当前用户是否已拉取完user_info信息
        console.log(getToken(), "getToken()11123444")
        store.dispatch('GetInfo').then(() => {
          // isRelogin.show = false
          store.dispatch('GenerateRoutes').then(accessRoutes => {
            // 根据roles权限生成可访问的路由表
            router.addRoutes(accessRoutes) // 动态添加可访问路由表
            next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
          })
        }).catch(err => {
          store.dispatch('LogOut').then(() => {
            Message.error(err)
            next({ path: '/' })
          })
        })
      } else {
        next()
      }
    // }
  } else {

    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    }  else if (to.path === '/casUI') {
      let ticket = to.query.jwt
      setToken(ticket)
      //commit('SET_TOKEN', ticket)
      window.location.href = "/index"
      NProgress.done()
    }else if(to.path === '/login'){
      // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
      NProgress.done()

    } else {
      // 线上
       next('index') // 否则全部重定向到登录页 杭电单点登录
       window.location.href = defaultSettings.casloginUrl;
       NProgress.done()

      // 测试 或其他学院登录
      // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
      // NProgress.done()

      //跳授权 无用
      // let url = window.location.href;
      // if (url.indexOf('?') != -1) {
      //   let obj = {};
      //   let arr = url.slice(url.indexOf('?') + 1).split('&');
      //   arr.forEach(item => {
      //     let param = item.split('=');
      //     obj[param[0]] = param[1];
      //   })
      //   console.log(obj);
      //   if (obj.state != undefined) {
      //     console.log(obj.state);
      //     //next('/index_v1.vue') league.ek.hdu.edu.cn localhost erke.hdu.edu.cn
      //   }
      // } else {
      //   // window.open('https://api.hduhelp.com/oauth/authorize?response_type=code&client_id=hHMZnpoSSTCnc6op&redirect_uri=https://erke.hdu.edu.cn/login&state=ah123456', '_self')
      //   // window.open('https://cas.hdu.edu.cn/cas/login?service=https://erke.hdu.edu.cn/login', '_self')
      //   //next('index') // 否则全部重定向到登录页
      //   window.location.href = defaultSettings.casloginUrl;
      //   NProgress.done()
      // }
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
